package com.example.actionsignin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.actionsignin.R;
import com.example.actionsignin.bean.RepeatHolder1;
import com.example.actionsignin.bean.RepeatHolder2;

import java.util.List;
import java.util.Map;

/**
 * Created by 旭 on 2017/8/5/005.
 */

public class RepeatAdapter extends BaseAdapter {

    private Context mContext;
    private List<Map<String,Object>> mList;
    private LayoutInflater mLayoutInflater;

    public RepeatAdapter(Context context, List<Map<String,Object>> list)
    {
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Map<String,Object> map = mList.get(position);

        View viewItem_1 = null;
        View viewItem_2 = null;

        int id = getItemViewType(position);
        if (id == RepeatHolder1.REPEAT_ITEM_TYPE)
        {
            RepeatHolder1 repeatHolder1 = null;
            if (convertView == null)
            {
                repeatHolder1 = new RepeatHolder1();

                viewItem_1 = mLayoutInflater.inflate(R.layout.list_item_repeat,parent,false);
                repeatHolder1.textView = (TextView) viewItem_1.findViewById(R.id.repeat_name);
                repeatHolder1.imageView = (ImageView) viewItem_1.findViewById(R.id.repeat_img);

                viewItem_1.setTag(repeatHolder1);
                convertView = viewItem_1;
            }else {
                repeatHolder1 = (RepeatHolder1) convertView.getTag();
            }

            repeatHolder1.textView.setText(String.valueOf(map.get("name")));
            if ((boolean)map.get("flag"))
            {
                repeatHolder1.imageView.setImageResource(R.mipmap.radio_ok);
            }else {
                repeatHolder1.imageView.setImageResource(R.mipmap.radio_no);
            }


        }else if (id == RepeatHolder2.REPEAT_ITEM_TYPE)
        {
            RepeatHolder2 repeatHolder2 = null;
            if (convertView == null)
            {
                repeatHolder2 = new RepeatHolder2();

                viewItem_2 = mLayoutInflater.inflate(R.layout.list_item_repeat_custom,parent,false);
                repeatHolder2.textView = (TextView) viewItem_2.findViewById(R.id.custom_date);
                repeatHolder2.checkBox = (CheckBox) viewItem_2.findViewById(R.id.custom_choose);

                viewItem_2.setTag(repeatHolder2);
                convertView = viewItem_2;
            }else {
                repeatHolder2 = (RepeatHolder2) convertView.getTag();
            }
            repeatHolder2.textView.setText(String.valueOf(map.get("date")));
            repeatHolder2.checkBox.setChecked((boolean) map.get("choose"));

//            repeatHolder2.checkBox.setChecked(false);
            repeatHolder2.checkBox.setClickable(false);
        }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return (int) mList.get(position).get("type");
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }
}
